package de.stromhaltig.grid;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;

public class XMPPConnector implements PacketListener, ChatManagerListener, MessageListener, CallbackHandler, ConnectionListener {
	Connection connection = null;
	private static XMPPConnector instance = new XMPPConnector();
	
	private XMPPConnector() {
		
	}
	
	
	public void authenticate() throws XMPPException {
		Connection conn1 = new XMPPConnection(Main.properties.getProperty("xmppserver"));
		conn1.connect();
		
		this.connection=conn1;
		ConnectionConfiguration config = new ConnectionConfiguration(Main.properties.getProperty("xmppserver"), 5222);
		 config.setRosterLoadedAtLogin(true);
		 SASLAuthentication.supportSASLMechanism("PLAIN", 0);
		 config.setSASLAuthenticationEnabled(true);
		this.connection.login(Main.properties.getProperty("deviceid"), Main.properties.getProperty("secret"));
		this.connection.addPacketListener(this,null);
		this.connection.addConnectionListener(this);
		this.connection.getChatManager().addChatListener(this);
	}
	
	public void register() throws XMPPException {
		if(this.connection.isConnected()==true) {
			this.connection.getAccountManager().createAccount(Main.properties.getProperty("deviceid"),Main.properties.getProperty("secret"));			
		} else {
			
			throw new RuntimeException("Not Connected:register()");
		}
	}
	
	public void presenceConnect() throws XMPPException {
		this.connection.getRoster().setSubscriptionMode( Roster.SubscriptionMode.accept_all);
		this.connection.getRoster().createEntry(Main.properties.getProperty("master"), "master", null);
	}
	
	public void updatePresence(boolean connected,int watt) {
		String status = ""+watt;
		Presence presence = null;
		if(connected) {
			presence = new Presence(Presence.Type.available,"ON",99,Presence.Mode.available);
		} else {
			presence = new Presence(Presence.Type.available,"OFF",99,Presence.Mode.away);
		}
		presence.setStatus(status);
		this.connection.sendPacket(presence);
	}
	
	public void connect() throws XMPPException {
		 
		 //config.setCallbackHandler(this);
		Connection conn1 = new XMPPConnection("gridswarm.stromhaltig.de");
		conn1.connect();
		
		this.connection=conn1;		
	}
	
	public static XMPPConnector getInstance() {
		
		return XMPPConnector.instance;
	}


	@Override
	public void processPacket(Packet arg0) {
		
		
	}

	

	@Override
	public void chatCreated(Chat arg0, boolean arg1) {
		arg0.addMessageListener(this);
		
	}


	@Override
	public void processMessage(Chat arg0, Message arg1) {
		if(arg1.getBody()!=null) {
			DeviceDialog.getInstance().lastrequest.setText(arg1.getBody());
			DeviceDialog.getInstance().btnAccept.setEnabled(true);
			DeviceDialog.getInstance().btnDecline.setEnabled(true);
			DeviceDialog.getInstance().setVisible(true);						
		}
	}
	
	public void sendMsgToMaster(String msg) {
		Message message = new Message();
		message.setTo(Main.properties.getProperty("master"));
		message.setBody(msg);
		this.connection.sendPacket(message);
	}


	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void connectionClosed() {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void connectionClosedOnError(Exception arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void reconnectingIn(int arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void reconnectionFailed(Exception arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void reconnectionSuccessful() {
		// TODO Auto-generated method stub
		
	}
}
